package com.dxl.lanju.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dxl.common.annotation.RequirePermission;
import com.dxl.common.enums.State;
import com.dxl.common.model.Order;
import com.dxl.common.model.Page;
import com.dxl.common.model.RespData;
import com.dxl.common.utils.Utils;
import com.dxl.lanju.service.ProductCateService;
import com.dxl.web.common.controller.BaseController;
import com.lanju.bean.ProdCateTreeNode;
import com.lanju.entity.ProductCate;

/**
 * 品类信息控制器
 * @author 邓小林
 */
@Controller
@RequestMapping("/productCate")
@RequirePermission(value={"PRODUCTCATE-MODULE"}, name="品类模块", desc="包括品类增、删、改、查")
public class ProductCateController extends BaseController {
	@Autowired
	private ProductCateService productCateService;
	
	@RequestMapping("")
	@RequirePermission(value={"PRODUCTCATE-VIEW"}, name="品类查看", desc="查看品类信息")
	public ModelAndView area() {
		ModelAndView mv = new ModelAndView("product/listCate");
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/list")
	@RequirePermission(value={"PRODUCTCATE-VIEW"}, name="品类查看", desc="查看品类信息")
	public Page<ProductCate> list(String keyword, Integer state, Integer page, Integer rows) {
		Page<ProductCate> p = this.productCateService.list(keyword, state, page, rows);
		return p;
	}
	
	@ResponseBody
	@RequestMapping("/tree")
	@RequirePermission(value={"PRODUCTCATE-VIEW"}, name="账户查看", desc="查看账户信息")
	public List<ProdCateTreeNode> tree() {
		List<ProdCateTreeNode> tree = this.productCateService.listTree();
		return tree;
	}
	
	@ResponseBody
	@RequestMapping("/listAll")
	@RequirePermission(value={"PRODUCTCATE-VIEW"}, name="品类查看", desc="查看品类信息")
	public List<ProductCate> listAll(Integer state) {
		return productCateService.listByProp(ProductCate.class, "state", Utils.isNull(state) ? State.ACTIVE.getState() : state, Order.asc("name"));
	}
	
	@RequestMapping("/edit")
	@RequirePermission(value={"PRODUCTCATE-VIEW"}, name="品类查看", desc="查看品类信息")
	public ModelAndView edit(String id) {
		ModelAndView mv = new ModelAndView("product/editCate");
		if (Utils.isNotBlank(id)) {
			ProductCate area = this.baseService.get(ProductCate.class, id);
			mv.addObject("entity", area);
		}
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/save")
	@RequirePermission(value={"PRODUCTCATE-MODIFY"}, name="品类修改", desc="修改品类信息")
	public RespData save(ProductCate area) {
		return productCateService.save(area);
	}
	
	@ResponseBody
	@RequestMapping("/del")
	@RequirePermission(value={"PRODUCTCATE-DEL"}, name="品类删除", desc="删除品类信息")
	public RespData del(String[] ids) {
		return productCateService.del(ids);
	}
	
	@ResponseBody
	@RequestMapping("/state")
	@RequirePermission(value={"PRODUCTCATE-STATE"}, name="品类状态修改", desc="修改品类状态")
	public RespData state(String[] ids, Integer state) {
		return this.state(ProductCate.class, ids, state);
	}
}
